home *** CD-ROM | disk | FTP | other *** search
- ;==========================================================================;
- ; Service subroutines ;
- ; ;
- ; Copyright 1983 by William E. Westfield. All rights reserved. ;
- ; Copyright 1986 by H. Roy Engehausen. All rights reserved. ;
- ; This software may be freely distributed and used, but it may not ;
- ; under any circumstances be sold by anyone other than the author. ;
- ; It may be distributed by a commercial company as long as it is ;
- ; for no cost. ;
- ; ;
- ; Permission is explicity granted to use this code as a model for ;
- ; other programs requiring interupt driven serial I/O as long as they ;
- ; carry this copyright notice. ;
- ;==========================================================================;
-
- ;==========================================================================;
- ; Get a buffer -- returns buffer segment in AX. 0 = no buffers available ;
- ; Uses AX ;
- ;==========================================================================;
-
- get_a_buffer:
-
- MOV AX,free_buffer_head ; Get head of buffer
-
- OR AX,AX ; Any there?
- JZ get_a_buffer_exit ; Nope.... Just return
-
- PUSH ES ; Save ES
- MOV ES,AX ; Point to the buffer we are about to use
- MOV ES,ES:buffer_next ; Point to the next buffer
- MOV free_buffer_head,ES ; This is now the head
- POP ES ; Restore ES
-
- get_a_buffer_exit:
-
- RET ; Bye bye
-
- ;==========================================================================;
- ; Free a buffer -- buffer segment in AX. ;
- ; Uses AX. ;
- ;==========================================================================;
-
- free_a_buffer:
-
- PUSH ES ; Save ES
- MOV ES,AX ;
- MOV AX,free_buffer_head ; Get head of current free chain
- MOV ES:buffer_next,AX ; Add the one being freed to the front!
- MOV free_buffer_head,ES ; This is now the head
- POP ES ; Restore ES
-
- RET ; Bye bye
-
- ;==========================================================================;
- ; Debugging service routine ;
- ;==========================================================================;
-
- dispchar:
-
- PUSH AX ; Save registers
- PUSH BX ;
- PUSH BP ;
- PUSH SI ;
- PUSH DI ;
-
- MOV BX,0 ; Select display page 0
- MOV AH,14 ; Function code for write
- INT 10H ; Call video driver in bios
-
- POP DI ; Restore regs
- POP SI ; Restore regs
- POP BP ; Restore regs
- POP BX ; Restore regs
- POP AX ; Restore regs
-
- RET ; To caller
-
- disphex:
-
- PUSH AX ; Save registers
- PUSH BX ;
- PUSH CX ;
- PUSH DX ;
- PUSH BP ;
- PUSH SI ;
- PUSH DI ;
-
- MOV CH,AL ; Save char
- MOV CL,4 ;
- SHR AL,CL ;
- SUB BH,BH ;
- MOV BL,AL ;
- MOV AL,CS:hex[BX] ;
- SUB BX,BX ; Select display page 0
- MOV AH,14 ; Function code for write
- INT 10H ; Call video driver in bios
- MOV BL,CH ;
- AND BL,0FH ;
- MOV AL,CS:hex[BX] ;
- SUB BX,BX ; Select display page 0
- MOV AH,14 ; Function code for write
- INT 10H ; Call video driver in bios
-
-
- POP DI ; Restore regs
- POP SI ; Restore regs
- POP BP ; Restore regs
- POP DX ; Restore regs
- POP CX ; Restore regs
- POP BX ; Restore regs
- POP AX ; Restore regs
-
- RET ; To caller
-
- hex DB '0123456789ABCDEF' ;
-
- ;==========================================================================;
- ; Random number generator ;
- ; Answer from 255 to 0 is in AL ;
- ;==========================================================================;
-
- r_seed DD 2531011 ; Current Random number seed
- r_cons DD 2531011 ; Constant to add to each generation
- r_mult DD 214013 ; Constant multiplier for each generation
- r_ans DQ ? ; Holds the 8-byte answer for multiplication
- ; (we only use the first six bytes). The
- ; algorithm for multiplying a 4-byte integer
- ; by a 4-byte integer is included in its
- ; entirety, but the instructions affecting
- ; the high-order byte are commented out.
-
- random:
-
- PUSH DX ; Save the work register
-
- MOV WORD PTR r_ans+4,0 ; Clear high-order answer words
- ; MOV WORD PTR r_ans+6,0 ; High-order word of ans is not used
-
- MOV AX, WORD PTR r_seed ; Get low order word of seed number (X)
- MUL WORD PTR r_mult ; Multiply by low order word of multipler
- MOV WORD PTR r_ans,AX ; Save low order answer
- MOV WORD PTR r_ans+2,DX ; Save high order answer
- MOV AX, WORD PTR r_seed ; Get low order word of seed number (X)
- MUL WORD PTR r_mult+2 ; Multiply by high order byte of multipler
- ADD WORD PTR r_ans+2,AX ; Add to existing result
- ADC WORD PTR r_ans+4,DX ;
- ; JNC nextmul
- ; INC WORD PTR r_ans+6 ; The high-order word is not used
- ;nextmul:
-
- MOV AX,WORD PTR r_seed+2 ; Get high order word of seed
- MUL WORD PTR r_mult ; Multiply by low order word of multipler
- ADD WORD PTR r_ans+2,AX ; Add to existing result
- ADC WORD PTR r_ans+4,DX ;
- ; JNC nextmulh
- ; INC WORD PTR r_ans+6 ; The high-order word is not used
- ;nextmulh:
- MOV AX,WORD PTR r_seed+2 ; Get high order word of seed.
- MUL WORD PTR r_mult+2 ; Multiply by high order word of multipler
- ADD WORD PTR r_ans+4,AX ; Add to existing result
- ; ADC WORD PTR r_ans+6,DX ; This word is not currently used in later
- ; calculations
-
- MOV AX,WORD PTR r_cons ; Add the constant to the result
- ADD WORD PTR r_ans,AX ;
- MOV AX,WORD PTR r_cons+2 ;
- ADC WORD PTR r_ans+2,AX ;
-
- MOV AX,WORD PTR r_ans+2 ; We will use the low order dword for real
- MOV WORD PTR r_seed+2,AX ; and save it for next time
- MOV DX,WORD PTR r_ans ; Get low order word of r_answer
- MOV WORD PTR r_seed,DX ; and save it for next time
-
- POP DX ; Restore the work register
-
- MOV AL,AH ; Use high order byte of r_ans+2
-
- RET ;
-